<template>
  <div>
    <a-modal v-model="visible" :title="title" :width="700" centered @ok="handleSubmit" destroyOnClose class="return-modal">
      <a-form v-if="type == 'deliver'">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="物流单号">
          <a-input v-model="logisticsNum" placeholder="请输入" allowClear />
        </a-form-item>
      </a-form>
      <div v-if="type == 'done'">
        <h3>box服务单编号：{{ boxSn }}</h3>
        <GoodsTable :tableData="data" :tableType="'test'"></GoodsTable>
      </div>
    </a-modal>
  </div>
</template>
<script>
import GoodsTable from './goodsTable'
import { specialSendBox, specialSignBox, finishBox, getSpecialFinishBoxDetails } from '@/api/box/order'

export default {
  components: {
    GoodsTable
  },
  data() {
    return {
      visible: false,
      title: '',
      labelCol: {
        xs: { span: 4 },
        sm: { span: 3 }
      },
      wrapperCol: {
        xs: { span: 12 },
        sm: { span: 12 }
      },
      boxId: '',
      type: '',
      askId: '',
      boxSn: '',
      logisticsNum: '',
      data: [
        {
          id: '1234561',
          name: '矿泉水 550ml',
          barcode: '12421432143214321',
          price: '2.00',
          num: '1',
          amount: '2.00'
        },
        {
          id: '1234562',
          name: '凉茶 300ml',
          barcode: '12421432143214322',
          price: '3.00',
          num: '2',
          amount: '6.00'
        },
        {
          id: '1234563',
          name: '好吃的薯片',
          barcode: '12421432143214323',
          price: '7.00',
          num: '4',
          amount: '28.00'
        },
        {
          id: '1234564',
          name: '特别好吃的蛋卷',
          barcode: '12421432143214324',
          price: '8.50',
          num: '3',
          amount: '25.50'
        },
        {
          id: '12344562',
          name: '凉茶 300ml',
          barcode: '12421432143214322',
          price: '3.00',
          num: '2',
          amount: '6.00'
        },
        {
          id: '12341563',
          name: '好吃的薯片',
          barcode: '12421432143214323',
          price: '7.00',
          num: '4',
          amount: '28.00'
        },
        {
          id: '12345264',
          name: '特别好吃的蛋卷',
          barcode: '12421432143214324',
          price: '8.50',
          num: '3',
          amount: '25.50'
        }
      ]
    }
  },
  methods: {
    show(type, id, askId, boxSn) {
      this.type = type
      this.boxId = id
      this.askId = askId
      this.boxSn = boxSn
      this.logisticsNum = ''
      switch (type) {
        case 'deliver':
          this.title = '测试发货'
          this.visible = true
          break
        case 'sign':
          let that = this
          this.$confirm({
            title: '确认测试签收?',
            content: h => <div style="color:red;">仅限测试使用签收！</div>,
            centered: true,
            onOk() {
              that.submitSign()
            },
            onCancel() {
              // console.log('Cancel')
            },
            class: 'test'
          })
          break
        case 'done':
          this.title = '测试完成'
          this.visible = true
          this.fetchList()
          break
      }
    },
    fetchList() {
      let params = {
        requestData: {
          boxId: this.boxId
        }
      }
      getSpecialFinishBoxDetails(params).then(res => {
        if (res.code == 0) {
          this.data = res.data
        } else {
          this.$message.warning(res.msg)
        }
      })
    },
    handleSubmit() {
      if (this.type == 'deliver') {
        // 发货
        this.submitDeliver()
      } else if (this.type == 'sign') {
        // 签收
      } else if (this.type == 'done') {
        // 完成
        this.submitFinish()
      }
    },
    submitDeliver() {
      let params = {
        requestData: {
          expressno: this.logisticsNum,
          id: this.boxId
        }
      }
      specialSendBox(params).then(res => {
        if (res.code == 0) {
          this.$message.success('测试发货 成功！')
          this.visible = false
          this.$emit('updateTable')
        } else {
          this.$message.warning(res.msg)
        }
      })
    },
    submitSign() {
      let params = {
        requestData: {
          boxId: this.boxId
        }
      }
      specialSignBox(params).then(res => {
        if (res.code == 0) {
          this.$message.success('测试签收 成功！')
          this.$emit('updateTable')
        } else {
          this.$message.warning(res.msg)
        }
      })
    },
    submitFinish() {
      let params = {
        requestData: {
          boxId: this.boxId,
          askId: this.askId
        }
      }
      finishBox(params).then(res => {
        if (res.code == 0) {
          this.$message.success('测试完成 成功！')
          this.visible = false
          this.$emit('updateTable')
        } else {
          this.$message.warning(res.msg)
        }
      })
    }
  }
}
</script>
